package com.japstudy.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.japstudy.dao.IWrongWordDao;
import com.japstudy.entity.WrongWord;
import com.japstudy.service.IWrongWordService;

@Service
public class WrongWordServiceImpl implements IWrongWordService{

	@Autowired
	private IWrongWordDao iWrongWordDao;

	@Override
	public int add(WrongWord wrongWord) {
		return iWrongWordDao.add(wrongWord);
	}

	@Override
	public void insert(WrongWord wrongWord) {
		WrongWord selWrongWord = iWrongWordDao.selectByUserIdAndWordId(wrongWord);
		if(selWrongWord == null) {
			iWrongWordDao.add(wrongWord);
		} else {
			selWrongWord.setWrongCount(selWrongWord.getWrongCount() + 1);
			selWrongWord.setMaster(0);
			iWrongWordDao.updateMaster(selWrongWord);
			iWrongWordDao.updateWrongCount(wrongWord);
		}
	}

	@Override
	public WrongWord selectByUserIdAndWordId(WrongWord wrongWord) {
		return iWrongWordDao.selectByUserIdAndWordId(wrongWord);
	}

	@Override
	public int updateMaster(WrongWord wrongWord) {
		return iWrongWordDao.updateMaster(wrongWord);
	}

	@Override
	public int updateWrongCount(WrongWord wrongWord) {
		return iWrongWordDao.updateWrongCount(wrongWord);
	}

	@Override
	public List<WrongWord> selectWrongWordOfTopTen(String userId) {
		return iWrongWordDao.selectWrongWordOfTopTen(userId);
	}
}
